package com.wintop.youyuan.services.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wintop.youyuan.dao.UserDao;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.UserServices;
import com.wintop.youyuan.util.CalculateUtil;
import com.wintop.youyuan.util.MD5Util;

@Service("userServices")
public class UserServicesImpl implements UserServices {
	private UserDao userDao;

	@Resource(name = "userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public User login(String userid, String password) {
		return userDao.getUser(userid, password);
	}

	@Override
	public User register(User user, int age) {
		user.setVip(1);
		userDao.addUser(user, CalculateUtil.getBirthday(age));
		return userDao.getUser(user.getUserid());
	}

	@Override
	public boolean isHaveUser(String userAccount) {
		User user = userDao.getUser(userAccount);
		if (user != null) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updatePwd(User user, String newPwd) {
		user.setPassword(MD5Util.md5Encode(newPwd));
		return userDao.updateUser(user);
	}

	@Override
	public boolean deleteUser(int id) {
		return userDao.deleteUser(id);
	}

	@Override
	public boolean updateUser(User user) {
		return userDao.updateUser(user);
	}

	@Override
	public User getUser(int id) {
		return userDao.getUser(id);
	}

	@Override
	public User getUser(String userid) {
		return userDao.getUser(userid);
	}

	@Override
	public List<User> getUsers() {
		return userDao.getUsers();
	}

}
